package com.ruoyi.web.controller.business.sundry;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.sundry.domain.JbCheats;
import com.ruoyi.business.sundry.domain.dto.JbCheatsDto;
import com.ruoyi.business.sundry.service.JbCheatsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 秘籍 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("秘籍")
@RestController
@RequestMapping("/cheats")
public class JbCheatsController extends BaseController
{
    @Autowired
    private JbCheatsService service;

    /**
     * 查询秘籍列表
     */
    @ApiOperation("查询秘籍列表")
    @PreAuthorize("@ss.hasPermi('cheats:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbCheats jbCheats)
    {
        LambdaQueryWrapper<JbCheats> select = new LambdaQueryWrapper<>();
        select.eq(StringUtils.isNotEmpty(jbCheats.getType()), JbCheats::getType, jbCheats.getType());
        select.eq(ObjectUtils.isNotEmpty(jbCheats.getCategoryId()), JbCheats::getCategoryId, jbCheats.getCategoryId());
        select.and(StringUtils.isNotBlank(jbCheats.getTitle()), a -> a.like(JbCheats::getTitle,
                jbCheats.getTitle()).or().like(JbCheats::getDescription,
                jbCheats.getTitle()));
        if (Constants.PUBLIC_NUMBER_1.toString().equals(page.getSortKey()))
        {
            select.orderByDesc(JbCheats::getCreateTime);
        } else
        {
            select.orderByDesc(JbCheats::getViewCount);
        }
        IPage<JbCheats> iPage = service.page(page.b(), select);
        return JoR.getDataTable(iPage);
    }

    /**
     * 查询秘籍列表
     */
    @ApiOperation("查询秘籍列表")
    @PreAuthorize("@ss.hasPermi('cheats:list')")
    @GetMapping("/managerList")
    public TableDataInfo managerList(PageParam page, JbCheatsDto jbCheats)
    {
        IPage<JbCheatsDto> iPage = service.managerList(page, jbCheats);
        return JoR.getDataTable(iPage);
    }

    /**
     * 导出秘籍列表
     */
    @ApiOperation("导出秘籍列表")
    @PreAuthorize("@ss.hasPermi('cheats:export')")
    @Log(title = "秘籍", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbCheats jbCheats)
    {
        LambdaQueryWrapper<JbCheats> select = new LambdaQueryWrapper<>();
        IPage<JbCheats> iPage = service.page(page.b(), select);

        List<JbCheats> list = iPage.getRecords();
        ExcelUtil<JbCheats> util = new ExcelUtil<JbCheats>(JbCheats.class);
        util.exportExcel(response, list, "秘籍数据");
    }

    /**
     * 获取秘籍详细信息
     */
    @ApiOperation("获取秘籍详细信息")
    @PreAuthorize("@ss.hasPermi('cheats:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        JbCheats byId = service.getById(id);
        JbCheats update = new JbCheats();
        update.setId(byId.getId());
        update.setViewCount(byId.getViewCount() + 1);
        service.updateById(update);
        return success(byId);
    }

    /**
     * 新增秘籍
     */
    @ApiOperation("新增秘籍")
    @PreAuthorize("@ss.hasPermi('cheats:add')")
    @Log(title = "秘籍", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbCheats jbCheats)
    {
        return toAjax(service.saveInfo(jbCheats));
    }

    /**
     * 新增案例
     */
    @ApiOperation("新增案例")
    @PreAuthorize("@ss.hasPermi('cheats:add')")
    @Log(title = "案例", businessType = BusinessType.INSERT)
    @PostMapping("addCase")
    public AjaxResult addCase(@RequestBody JbCheats jbCheats)
    {
        jbCheats.setType("2");
        return toAjax(service.save(jbCheats));
    }

    /**
     * 修改秘籍
     */
    @ApiOperation("修改秘籍")
    @PreAuthorize("@ss.hasPermi('cheats:edit')")
    @Log(title = "秘籍", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbCheats jbCheats)
    {
        return toAjax(service.updateById(jbCheats));
    }

    /**
     * 删除秘籍
     */
    @ApiOperation("删除秘籍")
    @PreAuthorize("@ss.hasPermi('cheats:remove')")
    @Log(title = "秘籍", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(service.deleteInfo(ids));
    }

    /**
     * 查询秘籍列表
     */
    @ApiOperation("查询秘籍列表")
    @PreAuthorize("@ss.hasPermi('cheats:list')")
    @GetMapping("/managerAmount")
    public AjaxResult managerAmount(PageParam page, JbCheatsDto jbCheats)
    {
        QueryWrapper<JbCheats> select = new QueryWrapper<>();
        select.select(" status ,count(1) as viewCount");
        select.lambda().eq(StringUtils.isNotEmpty(jbCheats.getType()), JbCheats::getType, jbCheats.getType())
                .eq(ObjectUtils.isNotEmpty(jbCheats.getCategoryId()), JbCheats::getCategoryId, jbCheats.getCategoryId())
                .like(StringUtils.isNotBlank(jbCheats.getTitle()), JbCheats::getTitle, jbCheats.getTitle())
                .eq(ObjectUtils.isNotEmpty(jbCheats.getStatus()), JbCheats::getStatus, jbCheats.getStatus())
                .ge(StringUtils.isNotBlank(page.getStartTime()), JbCheats::getCreateTime, page.getStartTime())
                .le(StringUtils.isNotBlank(page.getEndTime()), JbCheats::getCreateTime, page.getEndTime())
                .and(ObjectUtils.isNotEmpty(jbCheats.getCategoryIds()), a ->
                {
                    a.eq(JbCheats::getCategoryId, jbCheats.getCategoryIds().get(0));
                    for (Long cid : jbCheats.getCategoryIds())
                    {
                        a.or().eq(JbCheats::getCategoryId, cid);
                    }
                })
                .groupBy(JbCheats::getStatus);
        List<JbCheats> list = service.list(select);
        return success(list);
    }

}
